package zabbix

import (
	"encoding/json"
	"errors"
)

func MaintenanceCreateData() (string,map[string]interface{}) {
	method := "maintenance.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Sunday maintenance",
        "active_since": 1358844540,
        "active_till": 1390466940,
        "groupids": [
            "2"
        ],
        "timeperiods": [
            {
                "timeperiod_type": 3,
                "every": 1,
                "dayofweek": 64,
                "start_time": 64800,
                "period": 3600
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MaintenanceDeleteData() (string,map[string]interface{}) {
	method := "maintenance.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "3",
        "1"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MaintenanceGetData() (string,map[string]interface{}) {
	method := "maintenance.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectGroups": "extend",
        "selectTimeperiods": "extend"	
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MaintenanceUpdateData() (string,map[string]interface{}) {
	method := "maintenance.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "maintenanceid": "3",
        "hostids": [
            "10085",
            "10084"
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func MaintenanceCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MaintenanceCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MaintenanceDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MaintenanceDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MaintenanceGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MaintenanceGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MaintenanceUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MaintenanceUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



